const addressModel = require('../models/addressModel');
const create = async (ctx, next) => {
  const req = ctx.request.body;
  const userId = ctx.state.user.userId;
  const data = Object.assign({status: 0, createDate: new Date()}, req);
  data.userId = userId;
  const result = await addressModel.create(data);
  if (result) {
    ctx.body = {
      code: 1,
      msg: '添加成功！',
      data: result
    };
  }
}

const findByUserId = async (ctx, next) => {
  const userId = ctx.state.user.userId;
  const result = await addressModel.find({userId});
  if (result) {
    ctx.body = {
      code: 1,
      data: result
    };
  }
}

const update = async (ctx, next) => {
  const req = ctx.request.body;
  const result = await addressModel.update({
    _id: req._id
  }, req);
  if (result) {
    ctx.body = {
      code: 1,
      msg: '修改成功！',
      data: {}
    };
  }
}

module.exports = {
  create,
  update,
  findByUserId
};